package com.ken.callblock2.tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * class JSONObjectHelper provides helper method for getting value from JSON object.
 */
public class JSONObjectHelper {

    /**
     * build JSONObject from string
     * @param message
     * @return JSONObject
     */
    public static JSONObject getJSONObject(String message) {
        if (message == null) {
            return null;
        }

        try {
            return new JSONObject(message);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * get string value for specific attribute.
     * 
     * @param object
     * @param key
     * @return value if attribute exists, null if attribute doesn't exist.
     */
    public static String getStringValue(JSONObject object, String key) {
        try {
            return object.getString(key);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * get int value for specific attribute.
     * 
     * @param object
     * @param key
     * @return value if attribute exists, 0 if attribute doesn't exist.
     */
    public static int getIntValue(JSONObject object, String key) {
        try {
            return object.getInt(key);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * get long value for specific attribute.
     * 
     * @param object
     * @param key
     * @return value if attribute exists, 0 if attribute doesn't exist.
     */
    public static long getLongValue(JSONObject object, String key) {
        try {
            return object.getLong(key);
        } catch (Exception e) {
            return 0l;
        }
    }

    /**
     * get double value for specific attribute.
     * 
     * @param object
     * @param key
     * @return value if attribute exists, 0 if attribute doesn't exist.
     */
    public static double getDoubleValue(JSONObject object, String key) {
        try {
            return object.getDouble(key);
        } catch (Exception e) {
            return 0.0f;
        }
    }

    /**
     * get boolean value for specific attribute.
     * 
     * @param object
     * @param key
     * @return value if attribute exists, false if attribute doesn't exist.
     */
    public static boolean getBooleanValue(JSONObject object, String key) {
        try {
            return object.getBoolean(key);
        } catch (Exception e) {
            return Boolean.FALSE;
        }
    }

    /**
     * get JSON Object for specific attribute.
     * 
     * @param object
     * @param key
     * @return object if attribute exists, null if attribute doesn't exist.
     */
    public static JSONObject getJSONObjectValue(JSONObject object, String key) {
        try {
            return object.getJSONObject(key);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * get JSON Array for specific attribute.
     * 
     * @param object
     * @param key
     * @return object if attribute exists, null if attribute doesn't exist.
     */
    public static JSONArray getJSONArrayValue(JSONObject object, String key) {
        try {
            return object.getJSONArray(key);
        } catch (Exception e) {
            return null;
        }
    }
}
